package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.ExchangePriceSetting;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.basic.AgentService;
import cc.yugu.yhdw.webserver.service.hdw.AgencyService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangePriceSettingService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.SecureRandom;
import java.util.Date;
/**
 * 换电价格设置
 *
 * @Date:Created in 2018/8/4 16:24
 * @Author:
 * @Modified By: zxj 2018/8/4 16:24
 */
@Controller
@RequestMapping(value = "/security/hdw/exchange_price_setting")
public class ExchangePriceSettingController extends SecurityController {

    @Autowired
    ExchangePriceSettingService exchangePriceSettingService;
    @Autowired
    AgentService agentService;
    // 新增
    @Autowired
    AgencyService agencyService;

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_02_05.getValue());
    }
    /**
     * 换电价格设置
     * @Param [search, request]
     * @Return cc.yugu.yhdw.common.entity.json.PageResult
     * @Author zxj 2018/8/4
     */
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(ExchangePriceSetting search, HttpServletRequest request) {
        // 前端选择“所有”
        if (search.getAgencyId() != null && search.getAgencyId() == 0) {
            search.setAgencyId(null);
        }
        if (search.getAgentId() != null && search.getAgentId() == 0) {
            search.setAgentId(null);
        }
        // 如果是运营商登录
        if(getSessionUser(request).getType() == User.Type.AGENT.getValue()){
            search.setAgentId(getSessionUser(request).getAgentId());
        }else if(getSessionUser(request).getType() == User.Type.AGENCY.getValue()){
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
        return PageResult.successResult(exchangePriceSettingService.findPage(search));
    }

    @RequestMapping(value = "view.htm")
    public String view(Model model, Long id) {
        ExchangePriceSetting entity = exchangePriceSettingService.findAgency(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            entity.setAgentName(agentService.find(entity.getAgentId()).getAgentName());
            if(entity.getAgencyId() != null){
                entity.setAgencyName(agencyService.find(entity.getAgencyId()).getAgencyName());
            }
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/exchange_price_setting/view";
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "add.htm", method = RequestMethod.GET)
    public void add() {

    }
    /**
     * 新建，创建换电价钱套餐
     * 
     * @Date:Created in 2018/8/1 18:00
     * @Author:
     * @Modified By: zjn 2018/8/1 18:00
     */ 
    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(ExchangePriceSetting entity, HttpServletRequest request) {
        entity.setCreateTime(new Date());
        if(getSessionUser(request).getType() == User.Type.AGENT.getValue()){
            entity.setAgentId(getSessionUser(request).getAgentId());
        }
        if(getSessionUser(request).getType() == User.Type.AGENCY.getValue()){
            entity.setAgentId(getSessionUser(request).getAgentId());
            entity.setAgencyId(getSessionUser(request).getAgencyId());
        }
        return exchangePriceSettingService.insert(entity);
    }

    @RequestMapping(value = "edit.htm")
    public String editBasic(Model model, Long id) {
        // 修改查询方法
        ExchangePriceSetting entity = exchangePriceSettingService.findAgency(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/exchange_price_setting/edit";
    }

    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(ExchangePriceSetting entity) {
        return exchangePriceSettingService.update(entity);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "delete.htm")
    public ExtResult delete(Long id) {
        return exchangePriceSettingService.delete(id);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "select_price_setting.htm")
    public void selectPriceSetting(Model model, Integer agentId) {
        model.addAttribute("agentId", agentId);
    }
}
